<?php

namespace app\controller\admin\sky;

use laytp\controller\Backend;
use laytp\library\CommonFun;

/**
 * Class Strategy
 * @package app\controller\admin\sky
 */
class Strategy extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法


    public function _initialize()
    {
        $this->model = new \app\model\api\SkyStrategy();
    }

    //添加
    public function add()
    {
        try {
            $post     = $this->request->post();
            if(empty($post['season'])){
                throw new \Exception('请选择季节');
            }

            $saveData = [
                'season'     => $post['season'],
                'navBarTitle'     => $post['navBarTitle'],
                'taskIndexTitle'  => $post['taskIndexTitle'],
                'title1'      => $post['title1'],
                'title2'      => $post['title2'],
                'title3'      => $post['title3'],
                'title4'      => $post['title4'],
                'title5'      => $post['title5'],
                'title6'      => $post['title6'],
                'title7'      => $post['title7'] ?? '',
                'title8'      => $post['title8'] ?? '',
                'index1'      => $post['index1'],
                'index2'      => $post['index2'],
                'index3'      => $post['index3'],
                'index4'      => $post['index4'],
                'index5'      => $post['index5'],
                'index6'      => $post['index6'],
                'index7'      => $post['index7'] ?? '',
                'index8'      => $post['index8'] ?? '',
                'description'=> strip_tags($post['description'],'<p><img><video>'),
                'content1'   => json_encode(explode(',',$post['content1']),JSON_UNESCAPED_UNICODE),
                'content2'   => json_encode(explode(',',$post['content2']),JSON_UNESCAPED_UNICODE),
                'content3'   => json_encode(explode(',',$post['content3']),JSON_UNESCAPED_UNICODE),
                'content4'   => json_encode(explode(',',$post['content4']),JSON_UNESCAPED_UNICODE),
                'content5'   => json_encode(explode(',',$post['content5']),JSON_UNESCAPED_UNICODE),
                'content6'   => json_encode(explode(',',$post['content6']),JSON_UNESCAPED_UNICODE),
                'content7'   => json_encode(explode(',',$post['content7']),JSON_UNESCAPED_UNICODE),
                'content8'   => json_encode(explode(',',$post['content8']),JSON_UNESCAPED_UNICODE),

            ];
            $saveAll = $this->model->save($saveData);
            if (!$saveAll) throw new \Exception('保存失败');

            $this->clearCache();
            return $this->success('操作成功');
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');

        if(empty($post['season'])){
            throw new \Exception('请选择日期');
        }

        $row->season      = $post['season'];
        $row->navBarTitle      = $post['navBarTitle'];
        $row->taskIndexTitle   = $post['taskIndexTitle'];
        $row->title1      = $post['title1'];
        $row->title2      = $post['title2'];
        $row->title3      = $post['title3'];
        $row->title4      = $post['title4'];
        $row->title5      = $post['title5'];
        $row->title6      = $post['title6'];
        $row->title7      = $post['title7'] ?? '';
        $row->title8      = $post['title8'] ?? '';
        $row->index1      = $post['index1'];
        $row->index2      = $post['index2'];
        $row->index3      = $post['index3'];
        $row->index4      = $post['index4'];
        $row->index5      = $post['index5'];
        $row->index6      = $post['index6'];
        $row->index7      = $post['index7'] ?? '';
        $row->index8      = $post['index8'] ?? '';
        $row->description = strip_tags($post['description'],'<p><img><video>');

        $row->content1   = json_encode(explode(',',$post['content1']),JSON_UNESCAPED_UNICODE);
        $row->content2   = json_encode(explode(',',$post['content2']),JSON_UNESCAPED_UNICODE);
        $row->content3   = json_encode(explode(',',$post['content3']),JSON_UNESCAPED_UNICODE);
        $row->content4   = json_encode(explode(',',$post['content4']),JSON_UNESCAPED_UNICODE);
        $row->content5   = json_encode(explode(',',$post['content5']),JSON_UNESCAPED_UNICODE);
        $row->content6   = json_encode(explode(',',$post['content6']),JSON_UNESCAPED_UNICODE);
        $row->content7   = json_encode(explode(',',$post['content7']),JSON_UNESCAPED_UNICODE);
        $row->content8   = json_encode(explode(',',$post['content8']),JSON_UNESCAPED_UNICODE);

        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function info(){
        $id = $this->request->param('id');

        $info = $this->model->where('id',$id)->find()->toArray();
        $info['content1'] = json_decode($info['content1']) ?? [];
        $info['content2'] = json_decode($info['content2']) ?? [];
        $info['content3'] = json_decode($info['content3']) ?? [];
        $info['content4'] = json_decode($info['content4']) ?? [];
        $info['content5'] = json_decode($info['content5']) ?? [];
        $info['content6'] = json_decode($info['content6']) ?? [];
        $info['content7'] = json_decode($info['content7']) ?? [];
        $info['content8'] = json_decode($info['content8']) ?? [];

        return $this->success('获取成功', $info);
    }
}